package com.interfaces.module.classify.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.interfaces.module.article.service.ArticleService;
import com.interfaces.module.classify.domain.Classify;
import com.interfaces.module.classify.service.ClassifyService;
import com.interfaces.module.common.controller.CommonController;
import com.interfaces.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/classify")
public class ClassifyController extends CommonController<Classify> {
    @Autowired
    private ClassifyService classifyService;
    @Autowired
    private ArticleService articleService;
    @Override
    protected IService<Classify> getCoreService() {
        return this.classifyService;
    }
    @Override
    @GetMapping("/list")
    @ResponseBody
    public Result list(String where) {
        QueryWrapper<Classify> queryWrapper = new QueryWrapper<Classify>();
        if (StringUtils.isNotEmpty(where)) {
            if (where.contains("&")) {
                String[] whereDatas = where.split("&");
                for (String whereStr : whereDatas) {
                    String[] whereParam = whereStr.split("=");
                    queryWrapper.eq(whereParam[0], whereParam[1]);
                }
            } else {
                if (where.contains("=")) {
                    String[] whereParam = where.split("=");
                    queryWrapper.eq(whereParam[0], whereParam[1]);
                } else if (where.contains("=")) {
                    String[] whereParam = where.split("like");
                    queryWrapper.like(whereParam[0].trim(), whereParam[1].trim());
                }

            }
        }
        queryWrapper.orderByDesc("rank");
        List<Classify> dataList = getCoreService().list(queryWrapper);
        return Result.SUCCESS(dataList);
    }
}
